package com.xudow.app.dynamicstate_old.module.follow;

import android.support.annotation.NonNull;
import android.util.Log;

import com.jude.beam.expansion.list.BeamListFragmentPresenter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.SchedulerTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;

import java.util.concurrent.TimeUnit;

/**
 * Created by ske on 2016/6/10.
 */
public class FollowPresenter extends BeamListFragmentPresenter<FollowFragment, Dynamic> {
    long lastTime = 0;

    @Override
    protected void onCreateView(@NonNull FollowFragment view) {
        super.onCreateView(view);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        try {
            DynamicModel.getInstance()
                    .getDynamics(10, lastTime, 1)
                    .doOnNext(list -> {
                        if (list != null && list.size() > 0)
                            lastTime = list.get(list.size() - 1).getPublishTime();
                    })
                    .doAfterTerminate(() -> getView().stopRefresh())
               .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getRefreshSubscriber());
        }catch (Exception e){

            Log.i("reclerviuewbug","Exception"+e);

        }

    }

    @Override
    public void onLoadMore() {
        try {
        DynamicModel.getInstance()
                .getDynamics(10, lastTime, 2)
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getPublishTime();
                })
//                    .delay(1, TimeUnit.SECONDS)
//                    .compose(new SchedulerTransform<>())
                .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                .unsafeSubscribe(getMoreSubscriber());
    }catch (Exception e){

        Log.i("reclerviuewbug","Exception"+e);

    }
    }
}
